package com.yuexun.beilunpatient.ui.main.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;

import com.yuexun.beilunpatient.R;
import com.yuexun.beilunpatient.base.BaseEntity;
import com.yuexun.beilunpatient.baselistener.ICallBackListener;
import com.yuexun.beilunpatient.config.AppConfig;
import com.yuexun.beilunpatient.config.Contracts;
import com.yuexun.beilunpatient.ui.activity.ui.Act_Activity_Main;
import com.yuexun.beilunpatient.ui.doctor.ui.Act_Doc_Main;
import com.yuexun.beilunpatient.ui.doctorwarn.ui.Act_Doc_Warn;
import com.yuexun.beilunpatient.ui.main.adapter.MenuAdapter;
import com.yuexun.beilunpatient.ui.main.bean.MenuItemBean;
import com.yuexun.beilunpatient.ui.main.bean.TaskCountData;
import com.yuexun.beilunpatient.ui.main.model.impl.MessageUnreadModel;
import com.yuexun.beilunpatient.ui.main.presenter.ISysRemindPresenter;
import com.yuexun.beilunpatient.ui.main.presenter.impl.SysRemindPresenter;
import com.yuexun.beilunpatient.ui.main.ui.view.ISysRemindView;
import com.yuexun.beilunpatient.ui.question.ui.Act_Question;
import com.yuexun.beilunpatient.ui.registration.ui.Act_Registration_Tips;
import com.yuexun.beilunpatient.ui.satisfaction.ui.Act_Satisfaction_Main;
import com.yuexun.beilunpatient.ui.wait.ui.Act_Waiting_Information;

import org.kymjs.kjframe.ui.KJFragment;
import org.kymjs.kjframe.utils.PreferenceHelper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by chenss on 2018/3/13 0013.
 */

public class Frag_Interact extends KJFragment implements ISysRemindView {
    @Bind(R.id.health_menu)
    GridView healthMenu;

    MenuAdapter menuAdapter;

    private ISysRemindPresenter sysRemindPresenter;
    //总的红点
    private int sysCount = 0;

    ICallBackListener listener;

    public void setBackListener(ICallBackListener listener) {
        this.listener = listener;
    }

    @Override
    protected View inflaterView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
        View view = inflater.inflate(R.layout.frag_interact, null);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    protected void initData() {
        sysRemindPresenter = new SysRemindPresenter(new MessageUnreadModel(), this);

        menuAdapter = new MenuAdapter(healthMenu, Contracts.interactMenuList, R.layout.item_menu);
        healthMenu.setAdapter(menuAdapter);

        healthMenu.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                MenuItemBean item = (MenuItemBean) parent.getItemAtPosition(position);
                Intent intent;
                switch (item.getPosition()) {
                    case 0:
                        //预约挂号
                        startActivity(new Intent(getActivity(), Act_Registration_Tips.class));
                        break;
                    case 1:
                        //候诊信息
                        startActivity(new Intent(getActivity(), Act_Waiting_Information.class));
                        break;
                    case 3:
                        //医生提醒
                        startActivity(new Intent(getActivity(), Act_Doc_Warn.class));
                        break;
                    case 4:
                        //我要提问
                        startActivity(new Intent(getActivity(), Act_Question.class));
                        break;
                    case 5:
                        //我的医生
                        startActivity(new Intent(getActivity(), Act_Doc_Main.class));
                        break;
                    case 6:
                        //满意度调查
                        startActivity(new Intent(getActivity(), Act_Satisfaction_Main.class));
                        break;
                    case 7:
                        //参与活动
                        startActivity(new Intent(getActivity(), Act_Activity_Main.class));
                        break;
                    default:
                        break;
                }
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        Map<String, String> params = new HashMap<>();
        params.put("sessionUuidPatient", PreferenceHelper.readString(getActivity(), AppConfig.USER_INFO, AppConfig.SESSION_ID));
        sysRemindPresenter.getUnReadSysRemindCount(params);
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            Map<String, String> params = new HashMap<>();
            params.put("sessionUuidPatient", PreferenceHelper.readString(getActivity(), AppConfig.USER_INFO, AppConfig.SESSION_ID));
            sysRemindPresenter.getUnReadSysRemindCount(params);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void showSysRemindCount(BaseEntity<TaskCountData> messageCountBean) {
        if (messageCountBean.getStatus().equals("200")) {
            sysCount = 0;
            List<TaskCountData> countDatas = messageCountBean.getDatas();
            for (TaskCountData countData : countDatas) {
                if (countData.getRemindType() == 102) {
                    sysCount = sysCount + countData.getRemindCount();
                    for (MenuItemBean menuItemBean : Contracts.interactMenuList) {
                        if (menuItemBean.getPosition() == 4) {
                            menuItemBean.setCount(countData.getRemindCount());
                            menuAdapter.notifyDataSetChanged();
                        }
                    }
                }
            }

            if (listener != null) {
                Bundle bundle = new Bundle();
                bundle.putInt("count", sysCount);
                listener.callback(bundle);
            }
        }
    }
}
